package com.zsck.zsgy.util;

import android.content.Context;
import android.content.SharedPreferences;

import com.github.pwittchen.prefser.library.rx2.Prefser;
import com.unionpay.tsmservice.data.Constant;
import com.zsck.zsgy.ui.guide.bean.CurrentOrLastCityBean;
import com.zsck.zsgy.ui.guide.bean.KeyListBean;

import java.util.HashSet;
import java.util.Set;

public class PrefserHelper {
    private static Prefser sPrefser;

    private static String userKey = null;
    private static String userName = null;
    private static String version = null;

    private PrefserHelper() {
        throw new UnsupportedOperationException("u can't instantiate me...");
    }

    /**
     * 初始化默认的SharedPreferences文件
     */
    public static void initPrefser(Context context) {
        if (sPrefser == null) {
            sPrefser = new Prefser(context.getApplicationContext());
        }
    }

    /**
     * 清空默认的SharedPreferences文件
     */
    public static void clearPrefser() {
        sPrefser.clear();
    }

    public static void removeKey(String key) {
        sPrefser.remove(key);
    }


    /**
     * 获取自定义的SharedPreferences
     *
     * @param sp SharedPreferences文件
     */
    public static Prefser getPrefser(SharedPreferences sp) {
        return new Prefser(sp);
    }

    @SuppressWarnings("unchecked")
    public static HashSet<String> getCookies() {
        return (HashSet<String>) sPrefser.get(KEY_YJHF_COOKIES, Set.class, new HashSet<>());
    }

    public static void saveCookies(HashSet<String> cookies) {
        sPrefser.put(KEY_YJHF_COOKIES, cookies);
    }

    /**
     * 获取用户的密钥
     */
    public static String getUserKey() {
        if (userKey == null) {
            synchronized (PrefserHelper.class) {
                if (userKey == null) {
                    return userKey = sPrefser.get(KEY_USER_TOKEN, String.class, "");
                }
            }
        }
        return userKey;
    }

    /**
     * 保存用户密钥
     *
     * @param userKey 用户密钥
     */
    public static void saveUserKey(String userKey) {
        PrefserHelper.userKey = userKey;
        sPrefser.put(KEY_USER_TOKEN, userKey);
    }

    /**
     * 保存用户名
     *
     * @param userName 用户名
     */
    public static void saveUserName(String userName) {
        PrefserHelper.userName = userName;
        sPrefser.put(KEY_USER_NAME, userName);
    }

    /**
     * 获取用户名
     */
    public static synchronized String getUserName() {
        if (userName == null) {
            synchronized (PrefserHelper.class) {
                if (userName == null) {
                    return userName = sPrefser.get(KEY_USER_NAME, String.class, "");
                }
            }
        }
        return userName;
    }

    /**
     * 是否已经登录
     */
    public static boolean isLoggedIn() {
        return sPrefser.get(KEY_LOGGED_IN, Boolean.class, false);
    }

    /**
     * 设置是否已经登录
     */
    public static void setLoggedIn(boolean loggedIn) {
        sPrefser.put(KEY_LOGGED_IN, loggedIn);
    }

    //是否是初次打开
    public static boolean isFirst() {
        return sPrefser.get(ISFIRST, Boolean.class, false);
    }

    public static void setFirst(boolean first) {
        sPrefser.put(ISFIRST, first);
    }

    /**
     * 服务器版本
     */
    public static String getVersion() {
        if (version == null) {
            synchronized (PrefserHelper.class) {
                if (version == null) {
                    return version = sPrefser.get(VERSION, String.class, "");
                }
            }
        }
        return version;
    }


    /**
     * 服务器版本
     */
    public static void saveVersion(String version) {
        PrefserHelper.version = version;
        sPrefser.put(VERSION, version);
    }

    private static KeyListBean keyListBean=null;
    public static void saveKeyList(KeyListBean keyListBean){
        PrefserHelper.keyListBean=keyListBean;
        sPrefser.put(KEYLIST,keyListBean);
    }

    public static KeyListBean getKeyList() {
        if (keyListBean == null) {
            synchronized (PrefserHelper.class) {
                if (keyListBean == null) {
                    return keyListBean = sPrefser.get(KEYLIST, KeyListBean.class,null);
                }
            }
        }
        return keyListBean;
    }

    private static CurrentOrLastCityBean currentOrLastCityBean=null;
    public static void saveCurrentCity(CurrentOrLastCityBean currentOrLastCityBean){
        PrefserHelper.currentOrLastCityBean=currentOrLastCityBean;
        sPrefser.put(CURRENTCITY,currentOrLastCityBean);
    }

    public static CurrentOrLastCityBean getCurrentOrLastCityBean() {
        if (currentOrLastCityBean == null) {
            synchronized (PrefserHelper.class) {
                if (currentOrLastCityBean == null) {

                    return currentOrLastCityBean = sPrefser.get(CURRENTCITY, CurrentOrLastCityBean.class,null);
                }
            }
        }
        return currentOrLastCityBean;
    }




    private static final String KEY_YJHF_COOKIES = "yjhf_cookies";
    private static final String KEY_USER_TOKEN = "user_TOKEN";
    private static final String KEY_USER_NAME = "user_name";
    private static final String KEY_LOGGED_IN = "logged_in";
    private static final String ISFIRST = "ISFIRST";
    private static final String VERSION = "VERSION";
    private static final String KEYLIST="KEYLIST";
    private static final String CURRENTCITY="LOCATIONINFO";


}
